﻿#include "loginmeau.h"
#include "ui_loginmeau.h"
LoginMeau::LoginMeau(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginMeau)
{
    ui->setupUi(this);
    this->setWindowTitle("QQ");
    setAttribute(Qt::WA_DeleteOnClose, true);//关闭自动销毁
    ui->PwdText->setEchoMode(QLineEdit::Password);//不显示密码
    SocktetUtil::initSocket();
}

LoginMeau::~LoginMeau()
{
    qDebug()<<u8"LoginMeau 析构";
    exit(0);
    delete ui;
}
//登录
void LoginMeau::on_pushButton_clicked()
{
    if(ui->QqText->text().size() == 0){
        QMessageBox::information(this,u8"提示",u8"请输入qq");
        return;
    }
    if(ui->PwdText->text().size() == 0){
        QMessageBox::information(this,u8"提示",u8"请输入密码");
        return;
    }
    //连接服务器
    SOCKET sockCli = SocktetUtil::openClientSocket("47.113.150.167", 6543);//192.168.147.128
    //发送登录请求
    m.messageType = 0;
    strcpy(m.sender,ui->QqText->text().toStdString().c_str());
    strcpy(m.messageContent,ui->PwdText->text().toStdString().c_str());
    qDebug()<<send(sockCli,(char*)&m,sizeof(m),0);//发送
    qDebug()<<recv(sockCli,(char*)&m,sizeof(m),0);//等待接收服务端验证
    if(strncmp(m.messageContent,"T",2) == 0){
        qDebug()<<u8"登录成功";
        qDebug()<<sockCli;
        MainMeau* mm = new MainMeau(sockCli,ui->QqText->text(),ui->PwdText->text());
        this->hide();
    }else{
        QMessageBox::warning(this,u8"登录失败",u8"密码不正确！");
        closesocket(sockCli);
    }

}

void LoginMeau::on_register_2_clicked()
{
    //打开窗口
    Register* r = new Register;
    r->show();
}
